package com.android.sharecar.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.sharecar.model.CommentLevel1;
import com.android.sharecar.model.CommentLevel2;
import com.android.sharecar.model.User;
import com.android.sharecar.model.UserTalkMsg;
import com.android.sharecar.util.BitmapUtil;

/**
 * TalkListCommentsActivity shows user's comments about 
 *  others public talk..
 * @author xinyan
 * @date 2011-10-28
 */
public class TalkListCommentsActivity extends ExpandableListActivity {
	public static final String TAG = "TalkListCommentsActivity";
	private static final int DIALOG_TALK_REPLY = 1;
	private static final int DIALOG_COMMENT_REPLY = 2;

	// talk views
	private ImageView mHeadPic;
	private Button mReply;
	private TextView mNickName, mContent, mPostTime, mCommentNum;

	// comments: level 1 and level 2.
	private List<CommentLevel1> mGroup;
	private List<List<CommentLevel2>> mChild;
	private UserTalkMsg mTalkMsg; // talk
	private CommentAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_talk_comment1);

		findViews();
		setListenerForViews();

		getCommentsByTalkId(0);
		mAdapter = new CommentAdapter(this, mGroup, mChild);
		setListAdapter(mAdapter);
	}

	private void findViews() {
		mHeadPic = (ImageView) findViewById(R.id.talkCommentLevel1_iv_headPic);
		mNickName = (TextView) findViewById(R.id.talkCommentLevel1_tv_nickName);
		mContent = (TextView) findViewById(R.id.talkCommentLevel1_tv_comment);
		mPostTime = (TextView) findViewById(R.id.talkCommentLevel1_tv_postTime);
		mReply = (Button) findViewById(R.id.talkCommentLevel1_btn_reply);
		mCommentNum = (TextView) findViewById(R.id.talkCommentLevel1_tv_commentNum);
	}

	private void setListenerForViews() {
		mReply.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(DIALOG_TALK_REPLY);
			}
		});
	}

	/**
	 * Get comments of a talk by its id.
	 * @param talkId the talk's id
	 */
	private void getCommentsByTalkId(int talkId) {
		mGroup = new ArrayList<CommentLevel1>();
		mChild = new ArrayList<List<CommentLevel2>>();
		mTalkMsg = new UserTalkMsg();

		CommentLevel1 level1;
		CommentLevel2 level2;
		List<CommentLevel2> level2s;
		User user;
		Bitmap headPic;
		BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
				.getDrawable(R.drawable.myheadpic);
		headPic = bitmapDrawable.getBitmap();

		for (int i = 0; i < 8; i++) {
			user = new User();
			level1 = new CommentLevel1();
			level2s = new ArrayList<CommentLevel2>();

			user.setNickName("用户" + i);
			user.setHeadPicture(BitmapUtil.getBytes(headPic));

			level1.setUser(user);
			level1.setCommentNum(3);
			level1.setContent("静态的测试评论，哦哦");
			level1.setPostTime(new Date());
			mGroup.add(level1);

			for (int j = 0; j < 3; j++) {
				level2 = new CommentLevel2();

				level2.setUser(user);
				level2.setContent("静态的测试评论，哦哦");
				level2.setPostTime(new Date());

				level2s.add(level2);
			}
			mChild.add(level2s);
		}
	}

	// 自定义的ExpandListAdapter
	private final class CommentAdapter extends BaseExpandableListAdapter {
		private final Context mContext;
		private final List<CommentLevel1> mGroup;
		private final List<List<CommentLevel2>> mChild;

		/*
		* 构造函数:
		* 参数1:context对象
		* 参数2:一级列表数据源
		* 参数3:二级列表数据源
		*/
		public CommentAdapter(Context context, List<CommentLevel1> group,
				List<List<CommentLevel2>> child) {
			this.mGroup = group;
			this.mChild = child;
			this.mContext = context;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return mChild.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		// 获取二级列表的View对象
		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			Log.v(TAG, "ExpandableList ChildView..");

			CommentLevel2 comment = mChild.get(groupPosition)
					.get(childPosition);
			LayoutInflater layoutInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(
					R.layout.list_item_comment2, null);

			ImageView headPic;
			TextView nickName, commentContent, postTime;

			headPic = (ImageView) layout
					.findViewById(R.id.talkCommentLevel2_iv_headPic);
			nickName = (TextView) layout
					.findViewById(R.id.talkCommentLevel2_tv_nickName);
			commentContent = (TextView) layout
					.findViewById(R.id.talkCommentLevel2_tv_comment);
			postTime = (TextView) layout
					.findViewById(R.id.talkCommentLevel2_tv_postTime);

			User user = comment.getUser();

			if (null != user) {
				Log.v(TAG, "ExpandableListView  get user object...");

				headPic.setImageBitmap(BitmapUtil.getBitmap(user
						.getHeadPicture()));
				nickName.setText(user.getNickName());
			} else {
				Log.v(TAG, "ExpandableListView can't get user object...");
			}
			commentContent.setText(comment.getContent());
			postTime.setText(comment.getPostTime().toLocaleString());

			return layout;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return mChild.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mGroup.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return mGroup.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		// 获取一级列表View对象
		@Override
		public View getGroupView(final int groupPosition, // it should be final,
															// so we can get it
															// in reply button
															// listener.
				boolean isExpanded, View convertView, ViewGroup parent) {
			Log.v(TAG, "ExpandableList GroupView..");

			CommentLevel1 comment = mGroup.get(groupPosition);
			LayoutInflater layoutInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(
					R.layout.list_item_comment1, null);

			ImageView headPic;
			Button reply;
			TextView nickName, commentContent, postTime, commentNum, commentItem;

			headPic = (ImageView) layout
					.findViewById(R.id.list_item_talkCommentLevel1_iv_headPic);
			reply = (Button) layout
					.findViewById(R.id.list_item_talkCommentLevel1_btn_reply);
			nickName = (TextView) layout
					.findViewById(R.id.list_item_talkCommentLevel1_tv_nickName);
			commentContent = (TextView) layout
					.findViewById(R.id.list_item_talkCommentLevel1_tv_comment);
			postTime = (TextView) layout
					.findViewById(R.id.list_item_talkCommentLevel1_tv_postTime);
			commentNum = (TextView) layout
					.findViewById(R.id.list_item_talkCommentLevel1_tv_commentNum);
			commentItem = (TextView) layout.findViewById(R.id.comment);

			User user = comment.getUser();

			if (null != user) {
				headPic.setImageBitmap(BitmapUtil.getBitmap(user
						.getHeadPicture()));
				nickName.setText(user.getNickName());
			}
			commentContent.setText(comment.getContent());
			postTime.setText(comment.getPostTime().toLocaleString());
			if (comment.getCommentNum() != 0) {
				commentNum.setText(String.valueOf(comment.getCommentNum()));
			} else {
				commentNum.setVisibility(View.INVISIBLE);
				commentItem.setVisibility(View.INVISIBLE);
			}

			// If the button get the focus, the list won't expand any more.
			reply.setFocusable(false);
			reply.setClickable(true);
			reply.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					LayoutInflater factory = LayoutInflater
							.from(TalkListCommentsActivity.this);
					final View replyView = factory.inflate(
							R.layout.list_item_reply, null);

					new AlertDialog.Builder(TalkListCommentsActivity.this)
							.setTitle(R.string.talk_postAComment)
							.setView(replyView)
							.setPositiveButton(R.string.positive,
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int whichButton) {

											// User clicked OK so do some stuff
											CommentLevel2 level2;
											User user;
											Bitmap headPic;
											BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
													.getDrawable(
															R.drawable.myheadpic);
											headPic = bitmapDrawable
													.getBitmap();

											EditText commentText = (EditText) replyView
													.findViewById(R.id.talkList_et_reply);

											user = new User();
											level2 = new CommentLevel2();

											user.setNickName("用户 x");
											user.setHeadPicture(BitmapUtil
													.getBytes(headPic));

											level2.setUser(user);
											String strComment = commentText
													.getText().toString();
											if (null != strComment) {
												level2.setContent(strComment);
											}
											level2.setPostTime(new Date());

											mChild.get(groupPosition).add(
													level2);
											mAdapter.notifyDataSetChanged();
										}
									})
							.setNegativeButton(R.string.cancel,
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int whichButton) {

											// User clicked cancel so do some
											// stuff
											dialog.dismiss();
										}
									}).create().show();
				}
			});

			return layout;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		LayoutInflater factory = LayoutInflater
				.from(TalkListCommentsActivity.this);
		final View replyView = factory.inflate(R.layout.list_item_reply, null);

		switch (id) {
		case DIALOG_TALK_REPLY:
			return new AlertDialog.Builder(TalkListCommentsActivity.this)
					.setTitle(R.string.talk_postAComment)
					.setView(replyView)
					.setPositiveButton(R.string.positive,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked OK so do some stuff */
									CommentLevel1 level1;
									User user;
									Bitmap headPic;
									BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
											.getDrawable(R.drawable.myheadpic);
									headPic = bitmapDrawable.getBitmap();

									EditText commentText = (EditText) replyView
											.findViewById(R.id.talkList_et_reply);

									user = new User();
									level1 = new CommentLevel1();

									user.setNickName("用户 x");
									user.setHeadPicture(BitmapUtil
											.getBytes(headPic));

									level1.setUser(user);
									level1.setCommentNum(0);
									String strComment = commentText.getText()
											.toString();
									if (null != strComment) {
										level1.setContent(strComment);
									}
									level1.setPostTime(new Date());
									mGroup.add(level1);

									mAdapter.notifyDataSetChanged();
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked cancel so do some stuff */
									dialog.dismiss();
								}
							}).create();
		case DIALOG_COMMENT_REPLY:

			return new AlertDialog.Builder(TalkListCommentsActivity.this)
					.setTitle(R.string.talk_postAComment)
					.setView(replyView)
					.setPositiveButton(R.string.positive,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked OK so do some stuff */
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int whichButton) {

									/* User clicked cancel so do some stuff */
									dialog.dismiss();
								}
							}).create();
		}
		return null;
	}

}
